from app import  models,crud
from sqlmodel import Session
from app.core import security
from app.core.security import get_password_hash,verify_password
from app.models import  Token, LoginForm,TokenResponse
from fastapi.security import OAuth2PasswordRequestForm
from datetime import timedelta
from app.core.config import settings
class LoginService():  
    db: Session

    def __init__(self, db: Session):
        self.db = db  
    
    # async def authenticate(self, form_data: OAuth2PasswordRequestForm) -> TokenResponse:
    #     fileter_str={}
    #     # if form_data.client_id=='PHONE':
    #     fileter_str['phone']=form_data.username
    #     # elif form_data.client_id=='EMAIL':
    #     #     fileter_str['email']=form_data.username
    #     # else:
    #     #     fileter_str['thirdPartyId']=form_data.username    
    #     db_user = await crud.User(self.db).find_by(fileter_str)        
    #     if not db_user:
    #         return TokenResponse(code=400, msg="不正确的用户名",data=None)        
    #     if not verify_password(form_data.password, db_user.hashed_password):
    #         return TokenResponse(code=400, msg="不正确的密码",data=None)                
    
    #     access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    #     token = security.create_access_token(
    #             {"userId":db_user.id, 
    #              "phone":db_user.phone,
    #              "email":db_user.email,
    #              "thirdPartyId":db_user.thirdPartyId,
    #              "login_type":form_data.client_id},expires_delta=access_token_expires
    #         )
    #     return TokenResponse(
    #         data=Token(access_token=token),
    #         msg="获取成功",
    #         code =0        
    #     )
    
